import { Component, Input, OnInit, ViewChild } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { environment } from '@env/environment';
import { STChange, STClickRowClassNameType, STColumn, STComponent, STPage } from '@delon/abc/st';
import { DragModalService } from '../../../../../../shared/utils/drag-modal.service';
import { AuthDictService } from '../../../../../system/dict/service/dict.service';
import { CargoStorageService } from '../../cargo-storage.service';
import { TransportService } from '../../../../transport-service/transport.service';
import { CargoStorageShipDetailComponent } from '../detail/detail.component';

@Component({
  selector: 'app-cargo-storage-ship-list',
  templateUrl: './list.component.html',
  styleUrls: ['./list.component.less']
})
export class CargoStorageShipListComponent implements OnInit {
	@Input()
	preAreaId: string;

	searchForm: FormGroup;
	pageInfo = {
		total: 0,
		current: 1,
		size: 20,
		loading: false,
		nzPageSizeOptions: environment.nzPageSizeOptions
	};
	pageSetting: STPage = {
		total: '',
		show: true,
		showSize: true,
		front: false,
		pageSizes: environment.nzPageSizeOptions
	};
	@ViewChild('st', { static: false }) private st!: STComponent;
	columns: STColumn[] = [
		{
			title: '序号',
			width: 50,
			index: 'index',
			type: 'no',
			noIndex: (item, col, index) => index + 1 + (this.st.pi - 1) * this.st.ps,
			fixed: 'left',
			className: 'tc'
		},
		{
			title: '船名',
			index: 'shipNameZh',
			width: 80
		},
		{
			title: '航次',
			index: 'voyage',
			width: 80
		},
		{
			title: '进出口',
			index: 'ieName',
			width: 80
		},
		{
			title: '件数',
			index: 'allPiece',
			width: 80
		},
		{
			title: '吨数',
			index: 'allTon',
			width: 80
		},
		{
			title: '货类数',
			index: 'cargoTypes',
			width: 80
		},
		{
			title: '货主数',
			index: 'cargoOwners',
			width: 80
		},
		{
			title: '操作',
			className: 'tc',
			width: 100,
			fixed: 'right',
			render: 'manage'
		}
	];
	dataList = [];
	totalData: any;
	locationList = [];
	positionList = [];
	shipList = [];
	ieList = environment.dict.ie;
	constructor(
		private fb: FormBuilder,
		private modalService: DragModalService,
		private dictService: AuthDictService,
		private cargoStorageService: CargoStorageService,
		private transportService: TransportService
	) {
		this.locationList = this.dictService.dictMainLocationInfoList;
		this.positionList = this.dictService.dictLocationInfoList;
		this.transportService.transportShipList({}).subscribe((res:any) => {
			this.shipList = res;
		});
		this.ieList.push({ dictKey: 'CZ', dictValue: '存栈'});
		this.searchForm = this.fb.group({
			areaId: null,
			positionId: null,
			iE: null,
			transportShipId: null
		});
	}

	ngOnInit(): void {
		if (this.preAreaId !== null && this.preAreaId !== '' && this.preAreaId !== undefined) {
			this.locationList = this.dictService.dictMainLocationInfoList.filter(i => i.id === this.preAreaId);
			this.positionList = this.dictService.dictLocationInfoList.filter(i => i.areaId === this.preAreaId);
		}
		this.loadData();
	}

	loadData(): any {
		this.pageInfo.loading = true;
		const params = {
			preAreaId: this.preAreaId,
			...this.pageInfo,
			...this.searchForm.value
		};
		this.cargoStorageService.storageByShipPage(params).subscribe((res: any) => {
			this.dataList = res.records;
			this.pageInfo.total = res.total;
			this.pageSetting.total = res.total;
			this.pageInfo.loading = false;
			this.cargoStorageService.storageByShipTotal(params).subscribe((total: any) => {
				this.totalData = total;
			});
		});
	}

	search(): any {
		this.pageInfo.current = 1;
		this.loadData();
	}

	detail(record: any): any {
		const storageInfo = {
			preAreaId: this.preAreaId,
			transportShipId: record?.transportShipId,
			shipNameZh: record?.shipNameZh,
			voyage: record?.voyage
		}
		this.modalService
			.createDrag({
				nzContent: CargoStorageShipDetailComponent,
				nzTitle: storageInfo.shipNameZh + '-' + storageInfo.voyage,
				nzComponentParams: { storageInfo },
				nzWidth: '1200px',
				nzFooter: null
			})
			.afterClose.subscribe(() => {
			this.loadData();
		});
	}

	reset() {
		this.pageInfo.current = 1;
		this.searchForm.reset();
		this.loadData();
	}

	stChange(ret: STChange) {
		if (ret.type === 'pi' || ret.type === 'ps') {
			this.pageInfo.current = ret.pi;
			this.pageInfo.size = ret.ps;
			this.loadData();
		}
	}

	sa: STClickRowClassNameType = {
		fn(record: any, index: number): string {
			return 'selected-row';
		},
		exclusive: true
	};

	setPositionList(data: any): void {
		this.searchForm.patchValue({ position: null });
		if (data) {
			this.positionList = this.dictService.dictLocationInfoList.filter((i) => i.areaId === data);
		} else {
			this.positionList = this.dictService.dictLocationInfoList;
		}
	}
	searchShipList(data: string) {
		this.transportService.transportShipList({ keyword: data }).subscribe(res => {
			this.shipList = res;
		});
	}
}

